package com.gzk.org.controller;

import com.gzk.basic.util.AjaxResult;
import com.gzk.basic.util.PageList;
import com.gzk.org.domain.Department;
import com.gzk.org.query.DepartmentQuery;
import com.gzk.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    //dev1写的代码
    //dev2写的代码

    //注入
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/{id}")
    public Department getById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    @GetMapping("/list")
    public List<Department> getAll(){
        return departmentService.queryAll();
    }

    @PostMapping("/page")
    public PageList<Department> pageQuery(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @PostMapping("/save")
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        try {

            //判断
            if(department.getId() == null){
                //增加
                departmentService.add(department);
            }else {
                //修改
                departmentService.update(department);
            }
            //成功
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    @PatchMapping("/batch")
    public AjaxResult deleteMany(@RequestBody List<Long> ids){
        try {
            departmentService.removeMany(ids);
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    @PostMapping("/tree")
    public List<Department> getTree(){
        return departmentService.getTree();
    }


}
